//
//  AandeelAppDelegate.m
//  Aandeel
//
//  Created by Jin Hu on 2010/05/19.
//  Copyright __MyCompanyName__ 2010. All rights reserved.
//

#import "AandeelAppDelegate.h"
#import "RootViewController.h"
#import	"TFHpple.h"

NSString *kRestoreLocationKey = @"Aandelen-v0.3";	// preference key to obtain our restore location


@implementation AandeelAppDelegate

@synthesize window, navigationController, favoritesList,root ;


#pragma mark -
#pragma mark Application lifecycle


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    
	[self initData];
	//timer = [NSTimer scheduledTimerWithTimeInterval:0.01f target:self selector:@selector(loadData) userInfo:nil repeats:NO];
	
	[window addSubview:[navigationController view]];
    [window makeKeyAndVisible];
	return YES;
}


- (void)rip:(TFHpple *)doc to:(NSMutableArray*)list {
for (int i=0; i<[[doc search:@"//tr/td/div/img"] count]; i++) {
	NSString* search = [NSString stringWithFormat:@"//tr[%d]/td/div",(i*2)+1];
	NSArray *row  = [doc search:search];
	[row retain];

	NSMutableDictionary* cellModel;
	if([list count] <=i){
		cellModel = [NSMutableDictionary dictionaryWithCapacity:10];
		[list addObject:cellModel];
	}else{
		cellModel=[list objectAtIndex:i];
	}
	//TFHpple* name = [row objectAtIndex:1] ;
	//[cellModel setObject:@"xxxx" forKey:@"name"];
	//[cellModel setObject:[[row objectAtIndex:0] content] forKey:@"name"];
	[cellModel setObject:[[row objectAtIndex:2] content] forKey:@"price"];
	[cellModel setObject:[[row objectAtIndex:3] content] forKey:@"change"];
	[cellModel setObject:[[row objectAtIndex:4] content] forKey:@"percentage"];

	search = [NSString stringWithFormat:@"//tr[%d]/td/div/a",(i*2)+1];
	row  = [doc search:search];
	NSString* name = [[row objectAtIndex:0] content];
	if( name==NULL)
		name=@"A_X";
	[cellModel setObject:name forKey:@"name"];
	
}
}
- (void)applicationWillTerminate:(UIApplication *)application {
	if ([favoritesList count]==5) {
		[[NSUserDefaults standardUserDefaults] setObject:favoritesList forKey:kRestoreLocationKey];
	}
}
- (void)applicationWillResignActive:(UIApplication *)application {
	//if([timer isValid])
	//	[timer invalidate];	
	//[[NSUserDefaults standardUserDefaults] setObject:favoritesList forKey:kRestoreLocationKey];
}


- (void)applicationDidEnterBackground:(UIApplication *)application {
}


- (void)applicationWillEnterForeground:(UIApplication *)application {
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
	timer = [NSTimer scheduledTimerWithTimeInterval:0.01f target:self selector:@selector(loadData) userInfo:nil repeats:NO];
}

#pragma mark -
#pragma mark Memory management

- (void)dealloc {
	[navigationController release];
	[window release];
	[super dealloc];
}
- (void)setFavorite:(id)sender {
}
-(void)initData{
	// load the stored preference of the user's last location from a previous launch
	NSMutableArray *tempMutableCopy = [[[NSUserDefaults standardUserDefaults] objectForKey:kRestoreLocationKey] mutableCopy];
	favoritesList = tempMutableCopy;
	if(favoritesList==NULL || [favoritesList count]<5){
		favoritesList =[NSMutableArray arrayWithCapacity:4];
		NSMutableArray* aex =[NSMutableArray arrayWithCapacity:30];
		NSMutableArray* amx =[NSMutableArray arrayWithCapacity:30];
		NSMutableArray* ascx=[NSMutableArray arrayWithCapacity:30];
		NSMutableArray* favorite=[NSMutableArray arrayWithCapacity:90];
		[favoritesList addObject:aex];
		[favoritesList addObject:amx];
		[favoritesList addObject:ascx];
		[favoritesList addObject:favorite];
		[favoritesList retain];
		[aex retain];
		[amx retain];
		[ascx retain];
		[favorite retain];
		
	}else if ([favoritesList count]==5) {
		[favoritesList removeLastObject];
	}
}
-(void)loadData{
	
	NSURL *aUrl = [NSURL URLWithString:@"http://www.telegraaf.nl/dft/koersen/aandelen/?subtype=AEX"];
	NSData *data = [[NSData alloc] initWithContentsOfURL:aUrl];
	TFHpple* aexDoc = [[TFHpple alloc] initWithHTMLData:data];
	aUrl = [NSURL URLWithString:@"http://www.telegraaf.nl/dft/koersen/aandelen/?subtype=AMX"];
	data = [[NSData alloc] initWithContentsOfURL:aUrl];
	TFHpple* amxDoc = [[TFHpple alloc] initWithHTMLData:data];
	aUrl = [NSURL URLWithString:@"http://www.telegraaf.nl/dft/koersen/aandelen/?subtype=ASCX"];
	data = [[NSData alloc] initWithContentsOfURL:aUrl];
	TFHpple* ascxDoc = [[TFHpple alloc] initWithHTMLData:data];
	
	[aexDoc  retain];
	[amxDoc  retain];
	[ascxDoc retain];
	[self rip:aexDoc  to:[favoritesList objectAtIndex:0]];
	[self rip:amxDoc  to:[favoritesList objectAtIndex:1]];
	[self rip:ascxDoc to:[favoritesList objectAtIndex:2]];
	
	[root setModel:favoritesList];
	[root.view reloadData];
}


@end

